package ar.com.duam.porky.movil;

import static ar.com.duam.porky.movil.ConstantesPorky.DEFAULT_USERNAME;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

public class FormularioMovimientoActivity extends FragmentActivity implements OnItemSelectedListener
{
	private Movimiento movimiento;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_formulario_movimiento);

        SharedPreferences prefs = getSharedPreferences(ConstantesPorky.PREFS_NAME, MODE_PRIVATE);
        String username = prefs.getString("username", DEFAULT_USERNAME);

        PorkyOpenHelper helper = new PorkyOpenHelper(this);
       
        Usuario usuario = helper.getUsuario(username);
        
        Spinner concepto = (Spinner) this.findViewById(R.id.concepto);
        concepto.setOnItemSelectedListener(this);
        
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select "+ Concepto.COLUMN_ID +" as _id, "+ Concepto.COLUMN_NOMBRE +" from "+ 
        					Concepto.CONCEPTOS_TABLE_NAME +" where usuario_id = ? order by "+ Concepto.COLUMN_NOMBRE, new String[]{usuario.getId().toString()});

        String[] columns = new String[]{Concepto.COLUMN_NOMBRE};
        
        int[] to = new int[]{R.id.concepto_nombre};
        
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.concepto_entry, cursor, columns, to, 0);
        
        concepto.setAdapter(adapter);
        
        movimiento = new Movimiento();
        
        SimpleDateFormat sdf = new SimpleDateFormat("EEE dd/MM/yyyy");
        Button fecha = (Button) this.findViewById(R.id.fecha);
        fecha.setText(sdf.format(movimiento.getFecha()));
    }

    public void showDatePickerDialog(View v) 
    {
        DialogFragment newFragment = new DatePickerFragment()
        {
			public void onDateSet(DatePicker view, int year, int month, int day) 
			{
				Calendar date = Calendar.getInstance();
				date.set(Calendar.YEAR, year);
				date.set(Calendar.MONTH, month);
				date.set(Calendar.DATE, day);
				
				movimiento.setFecha(date.getTime());

		        SimpleDateFormat sdf = new SimpleDateFormat("EEE dd/MM/yyyy");
		        Button fecha = (Button) FormularioMovimientoActivity.this.findViewById(R.id.fecha);
		        fecha.setText(sdf.format(movimiento.getFecha()));
			}        	
        };
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }
    
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 
	{
		movimiento.setConceptoId((int) id);
	}

	public void onNothingSelected(AdapterView<?> parent) 
	{
	}
    
	public void grabar(View view)
	{
		EditText detalle = (EditText) this.findViewById(R.id.detalle);
		movimiento.setDetalle(detalle.getText().toString());
		
		EditText importe = (EditText) this.findViewById(R.id.importe);
		movimiento.setImporte(Float.parseFloat(importe.getText().toString()));
		
		Log.d("grabar", "Grabando el movimiento ["+ movimiento.toString() +"]");
		
		PorkyOpenHelper helper = new PorkyOpenHelper(this);
		helper.addMovimiento(movimiento);

    	Intent intent = new Intent(this, ListadoMovimientosActivity.class);
    	startActivity(intent);
	}
	
    public void cancelar(View view)
    {
    	Intent intent = new Intent(this, ListadoMovimientosActivity.class);
    	startActivity(intent);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_formulario_movimiento, menu);
        return true;
    }

}
